home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / gl_overlay.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.0 KB  |  156 lines

  1. //
  2. // "$Id: gl_overlay.cxx,v 1.4 1999/01/07 19:17:54 mike Exp $"
  3. //
  4. // OpenGL overlay test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <config.h>
  27. #include <FL/Fl.H>
  28. #include <FL/Fl_Window.H>
  29. #include <FL/Fl_Hor_Slider.H>
  30. #include <FL/Fl_Toggle_Button.H>
  31. #include <FL/math.h>
  32.  
  33. #if !HAVE_GL
  34. #include <FL/Fl_Box.H>
  35. class shape_window : public Fl_Box {
  36. public:    
  37.   int sides;
  38.   shape_window(int x,int y,int w,int h,const char *l=0)
  39.     :Fl_Box(FL_DOWN_BOX,x,y,w,h,l){
  40.       label("This demo does\nnot work without GL");
  41.   }
  42. };
  43. #else
  44. #include <FL/gl.h>
  45. #include <FL/Fl_Gl_Window.H>
  46.  
  47. class shape_window : public Fl_Gl_Window {
  48.   void draw();
  49.   void draw_overlay();
  50. public:
  51.   int sides;
  52.   int overlay_sides;
  53.   shape_window(int x,int y,int w,int h,const char *l=0);
  54. };
  55.  
  56. shape_window::shape_window(int x,int y,int w,int h,const char *l) :
  57. Fl_Gl_Window(x,y,w,h,l) {
  58.   sides = overlay_sides = 3;
  59. }
  60.  
  61. void shape_window::draw() {
  62. // the valid() property may be used to avoid reinitializing your
  63. // GL transformation for each redraw:
  64.   if (!valid()) {
  65.     valid(1);
  66.     glLoadIdentity();
  67.     glViewport(0,0,w(),h());
  68.   }
  69. // draw an amazing but slow graphic:
  70.   glClear(GL_COLOR_BUFFER_BIT);
  71.   //  for (int j=1; j<=1000; j++) {
  72.     glBegin(GL_POLYGON);
  73.     for (int i=0; i<sides; i++) {
  74.       double ang = i*2*M_PI/sides;
  75.       glColor3f(float(i)/sides,float(i)/sides,float(i)/sides);
  76.       glVertex3f(cos(ang),sin(ang),0);
  77.     }
  78.     glEnd();
  79.   // }
  80. }
  81.  
  82. void shape_window::draw_overlay() {
  83. // the valid() property may be used to avoid reinitializing your
  84. // GL transformation for each redraw:
  85.   if (!valid()) {
  86.     valid(1);
  87.     glLoadIdentity();
  88.     glViewport(0,0,w(),h());
  89.   }
  90. // draw an amazing graphic:
  91.   gl_color(FL_RED);
  92.   glBegin(GL_LINE_LOOP);
  93.   for (int i=0; i<overlay_sides; i++) {
  94.     double ang = i*2*M_PI/overlay_sides;
  95.     glVertex3f(cos(ang),sin(ang),0);
  96.   }
  97.   glEnd();
  98. }
  99. #endif
  100.  
  101. // when you change the data, as in this callback, you must call redraw():
  102. void sides_cb(Fl_Widget *o, void *p) {
  103.   shape_window *sw = (shape_window *)p;
  104.   sw->sides = int(((Fl_Slider *)o)->value());
  105.   sw->redraw();
  106. }
  107.  
  108. #if HAVE_GL
  109. void overlay_sides_cb(Fl_Widget *o, void *p) {
  110.   shape_window *sw = (shape_window *)p;
  111.   sw->overlay_sides = int(((Fl_Slider *)o)->value());
  112.   sw->redraw_overlay();
  113. }
  114. #endif
  115. #include <stdio.h>
  116. int main(int argc, char **argv) {
  117.  
  118.   Fl_Window window(300, 370);
  119.  
  120.   shape_window sw(10, 75, window.w()-20, window.h()-90);
  121. //sw.mode(FL_RGB);
  122.   window.resizable(&sw);
  123.  
  124.   Fl_Hor_Slider slider(60, 5, window.w()-70, 30, "Sides:");
  125.   slider.align(FL_ALIGN_LEFT);
  126.   slider.callback(sides_cb,&sw);
  127.   slider.value(sw.sides);
  128.   slider.step(1);
  129.   slider.bounds(3,40);
  130.  
  131.   Fl_Hor_Slider oslider(60, 40, window.w()-70, 30, "Overlay:");
  132.   oslider.align(FL_ALIGN_LEFT);
  133. #if HAVE_GL
  134.   oslider.callback(overlay_sides_cb,&sw);
  135.   oslider.value(sw.overlay_sides);
  136. #endif
  137.   oslider.step(1);
  138.   oslider.bounds(3,40);
  139.  
  140.   window.end();
  141.   window.show(argc,argv);
  142. #if HAVE_GL
  143.   printf("Can do overlay = %d\n", sw.can_do_overlay());
  144.   sw.show();
  145.   sw.redraw_overlay();
  146. #else
  147.   sw.show();
  148. #endif
  149.  
  150.   return Fl::run();
  151. }
  152.  
  153. //
  154. // End of "$Id: gl_overlay.cxx,v 1.4 1999/01/07 19:17:54 mike Exp $".
  155. //
  156.